using UnityEngine;

namespace UTJ.FrameCapturer
{
	public class GifEncoder : MovieEncoder
	{
		private fcAPI.fcGifContext m_ctx;

		private fcAPI.fcGifConfig m_config;

		public override Type type => Type.Gif;

		public override void Release()
		{
			m_ctx.Release();
		}

		public override bool IsValid()
		{
			return m_ctx;
		}

		public override void Initialize(object config, string outPath)
		{
			if (!fcAPI.fcGifIsSupported())
			{
				UnityEngine.Debug.LogError("Gif encoder is not available on this platform.");
				return;
			}
			m_config = (fcAPI.fcGifConfig)config;
			m_config.numColors = Mathf.Clamp(m_config.numColors, 1, 256);
			m_ctx = fcAPI.fcGifCreateContext(ref m_config);
			string path = outPath + ".gif";
			fcAPI.fcStream stream = fcAPI.fcCreateFileStream(path);
			fcAPI.fcGifAddOutputStream(m_ctx, stream);
			stream.Release();
		}

		public override void AddVideoFrame(byte[] frame, fcAPI.fcPixelFormat format, double timestamp)
		{
			if ((bool)m_ctx)
			{
				fcAPI.fcGifAddFramePixels(m_ctx, frame, format, timestamp);
			}
		}

		public override void AddAudioSamples(float[] samples)
		{
		}
	}
}
